<?php

class Provider extends CI_Controller {

	public function __construct() {
		parent::__construct();
	}

	function componentCss() {

		$sOutput = '';
		$this->load->helper('file');
		$aFiels = get_filenames('./resources/css/');
		natcasesort($aFiels);
		foreach ($aFiels as $sFile) {
			$aFileData = pathinfo($sFile);

			if ($aFileData["extension"] == "css") {
				$sOutput .= '/* '.$sFile."*/\n";
				$sOutput .= read_file('./resources/css/'.$sFile)."\n\n";
			}
		}
		header("Content-type: text/css");
		echo $sOutput;
		die();
	}

	function allowed_components() {

		$sOutput = '';
		$this->load->helper('file');
		$bSendPack = $this->config->item("send_pack");
		echo $bSendPack;
		$sFileProd = APPPATH."views/component/prod/";
		$sFileDebug = APPPATH."views/component/debug/";
		$aFiels = get_filenames($sFileDebug);
		foreach ($aFiels as $sFile) {
			$aFileData = pathinfo($sFile);

			if ($aFileData["extension"] != "js") {
				continue;
			}

			if ($bSendPack) {
				$script = file_get_contents($sFileDebug.$sFile);
				$filemtime = @filemtime($sFileDebug.$sFile);
				$filemtimeCache = @filemtime($sFileProd.$sFile);
				$t1 = microtime(true);

				if ((! $filemtimeCache) || ($filemtime > $filemtimeCache)) {
					$packer = new JavaScriptPacker($script, 'Normal', true, false);
					$packed = $packer->pack();
					file_put_contents($sFileProd.$sFile, $packed);
				}
				$t2 = microtime(true);
				$time = sprintf('%.4f', ($t2 - $t1));
				$sOutput .= '/* Script '.$sFile.' packed in '.$time." s. */\n";
				$sOutput .= read_file($sFileProd.$sFile)."\n\n";
			} else {
				$sOutput .= read_file($sFileDebug.$sFile)."\n\n";
			}
		}
		header("Content-type: application/javascript");
		echo $sOutput;
		die();
	}

	function extensions() {

		$sOutput = '';
		$this->load->helper('file');
		$aFiels = get_filenames('./resources/js/combine/');
		natcasesort($aFiels);
		foreach ($aFiels as $sFile) {
			$aFileData = pathinfo($sFile);

			if ($aFileData["extension"] == "js") {
				$sOutput .= '/* '.$sFile."*/\n";
				$sOutput .= read_file('./resources/js/combine/'.$sFile)."\n\n";
			}
		}
		$aFiels = get_filenames('./resources/js/customs/');
		natcasesort($aFiels);
		foreach ($aFiels as $sFile) {
			$sOutput .= '/* '.$sFile."*/\n";
			$sOutput .= read_file('./resources/js/customs/'.$sFile)."\n\n";
		}
		header("Content-type: application/javascript");
		echo $sOutput;
		die();
	}
}

/* End of file JsProvider.php */
/* Location: ./application/controllers/JsProvider.php */
